

function CreateSeparation () {
    this.age = 18
    this.name = 'CreateSeparation'
    this.idData = {
        addView : 'action_ll_add',
        searchApp : 'ed_search',
        home : 'tv_home',
        searchAppId : 'func_title',
        closeWindow : 'iv_close',
        launch : 'btn_launch_local',
        //容器列表
        box : 'rv_app_box',
        //设置
        setting : 'btn_vb_home_item_setting',
        //分身名字
        app_name : 'item_app_name',
        //卸载

         

    }

    this.stringData = {
        appName : '企业微信',
        stopRun : '停止运行',
        appName_2 : '悟空多开分身',
        home : '主页',
        uninstall : '卸载分身',
        alertTitle : '删除应用',
        delete : '删除'

    }

    this.numberData = {
        sleep : 1000,
        sum : 20
    }


    CreateSeparation.prototype.run = function () {

        let i = 0,
        eventsToast = false,
        compatibleThread = threads.start(function(){}),
        eventsThread = threads.start(function(){}),
        appName = this.stringData.appName,
        closeWindow = this.idData.closeWindow

        //开始监听toast
        eventsThread = threads.start(function () {
            events.observeToast();
            events.onToast(function(toast){
                // log("Toast内容: " + toast.getText() + " 包名: " + toast.getPackageName());
                if (toast.getText() == '安装'.concat(appName, '成功')) {
                    eventsToast = true
                }
            });
        })

        //开始兼容线程
        compatibleThread = threads.start(function () {
            while (1) {
                //关闭更新弹窗
                if (id(closeWindow).exists()) {
                    while(id(closeWindow).click());
                }
                sleep(1000)
            }
        })
        
        //开始创建分身
        print('开始')
        
        do {
            print('开始第%d次循环', i+1)
            id(this.idData.home).waitFor()
            print('主页\n点击添加应用')
            while(!id(this.idData.addView).findOne().click());
            print('等待进入【添加应用】界面')
            id(this.idData.searchApp).waitFor()
            print('搜索%s', this.stringData.appName)
            id(this.idData.searchApp).findOne().setText(this.stringData.appName)
            print('开始创建')
            while(!id(this.idData.searchAppId).text(this.stringData.appName).findOne().parent().click());
            print('等待创建成功')
            while(!eventsToast);
            print('%s分身创建成功', this.stringData.appName)
            i++
            sleep(this.numberData.sleep)
        } while(i <= this.numberData.sum);

        if (compatibleThread.isAlive()) compatibleThread.interrupt()
        if (eventsThread.isAlive()) eventsThread.interrupt()
        
        print('创建分身结束')
    }
}

module.exports = CreateSeparation
// let cs = new CreateSeparation()
// cs.run()
